package com.tsugun.mediaplayer.view.adapter;

import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.tsugun.mediaplayer.view.loader.MediaImageLoad;
import com.tsugun.mediaplayer.view.widget.ViewSwitcher;

/**
 * 图片切换器适配器
 * Created by shize on 2017/4/10.
 */

public class SwitcherAdapter extends PagerAdapter {

    private int[] mImgId;
    private Context mContext;
    private ViewSwitcher mSwitcher;

    public SwitcherAdapter(Context context, int[] imgId, ViewSwitcher switcher) {
        mImgId = imgId;
        mContext = context;
        mSwitcher = switcher;
    }

    @Override
    public int getCount() {
        return mImgId.length;
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        ImageView imageView = new ImageView(mContext);
//        imageView.setImageResource(mImgId[position]);
        MediaImageLoad.loadDrawableById(mImgId[position], imageView);
        imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
        imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
        container.addView(imageView);
        mSwitcher.setViewForPosition(imageView, position);
        return imageView;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((View) object);
    }
}
